package com.jojo.elementary.day12_tree;

import com.jojo.elementary.entity.TreeNode;

/**
 * 226. 翻转二叉树
 *
 * 给你一棵二叉树的根节点 root ，翻转这棵二叉树，并返回其根节点。
 *
 * 示例 1：
 * 输入：root = [4,2,7,1,3,6,9]
 * 输出：[4,7,2,9,6,3,1]
 *
 * 示例 2：
 * 输入：root = [2,1,3]
 * 输出：[2,3,1]
 *
 * 示例 3：
 * 输入：root = []
 * 输出：[]
 */
public class FlipTree {

    /** myCode 递归 */
    public TreeNode invertTree(TreeNode root) {
        if (root == null){
            return null;
        }
        invertTree(root.left);
        invertTree(root.right);
        exchange(root);
        return root;
    }

    void exchange(TreeNode root){
        TreeNode temp = root.left;
        root.left = root.right;
        root.right = temp;
    }

    public static void main(String[] args) {
        TreeNode root = new TreeNode(4);
        root.left = new TreeNode(2,new TreeNode(1),new TreeNode(3));
        root.right = new TreeNode(7,new TreeNode(6),new TreeNode(9));
        FlipTree flipTree = new FlipTree();
        TreeNode treeNode = flipTree.invertTree(root);
        System.out.println(treeNode.val);
        System.out.println(treeNode.left.val);
        System.out.println(treeNode.right.val);
        System.out.println(treeNode.left.left.val);
        System.out.println(treeNode.left.right.val);
        System.out.println(treeNode.right.left.val);
        System.out.println(treeNode.right.right.val);
    }
}
